﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Xml;
using RuleCode.ObjectTree ;

namespace SetupAction
{
    public partial class GenerateInfoXml : Form
    {
        public GenerateInfoXml()
        {
            InitializeComponent();
        }

        private void Button_OpenFolder_Click(object sender, EventArgs e)
        {
            this.folderBrowserDialog1  .ShowDialog();
            if (folderBrowserDialog1.SelectedPath  != String.Empty)
            {
                TextBox_GeneratePath.Text = folderBrowserDialog1.SelectedPath; 
            }
        }

        private void Button_Generate_Click(object sender, EventArgs e)
        {
            string setupFilePath = TextBox_GeneratePath.Text.Trim();
            if (!String.IsNullOrEmpty(setupFilePath))
            {
                //创建 info.xml 
                string infoFileName = Path.Combine(setupFilePath, "info.xml");
                if (!File.Exists(infoFileName))
                    File.Create(infoFileName);
                FileInfo fInfo = new FileInfo(infoFileName);
                fInfo.Attributes = FileAttributes.Hidden | fInfo.Attributes;


                RuleCode.Common.Cryptography cry = new RuleCode.Common.Cryptography();
                XmlDocument infoXmlDoc = new XmlDocument();
                XmlNode infoNode = infoXmlDoc.CreateElement("info");
                XmlNode sDateNode = infoXmlDoc.CreateElement("sdate");
                string setupdateStr = cry.DesEncrypt(DateTime.Now.ToString(), Util.CryKey);
                sDateNode.InnerText = DateTimePicker_Setup .Value .ToString ();
                infoNode.AppendChild(sDateNode);

                XmlNode lDateNode = infoXmlDoc.CreateElement("ldate");
                lDateNode.InnerText = DateTimePicker_Last .Value.ToString ();
                infoNode.AppendChild(lDateNode);

                XmlNode vDayNode = infoXmlDoc.CreateElement("vday");
                string validateDays = cry.DesEncrypt(TextBox_Days.Text.Trim (), Util.CryKey);
                vDayNode.InnerText = validateDays;
                infoNode.AppendChild(vDayNode);

                infoXmlDoc.AppendChild(infoNode);
                infoXmlDoc.Save(infoFileName);      
            }
        }
    }
}
